<template>
  <!-- 服务商管理 -->
  <div>
    <custom-container ref="customContainerRef" :config="mainConfig"></custom-container>
    <setForm
      ref="setFormRef"
      v-model:title="title"
      v-model:open="open"
      @changeList="handleQuery"
    ></setForm>
  </div>
</template>
<script lang="ts" setup>
import { computed, ref, defineAsyncComponent, getCurrentInstance, nextTick } from 'vue'
import { Plus, Edit, Delete, Search, Upload, Bottom, Key, UploadFilled } from '@element-plus/icons-vue'
import { ElButton, ElMessageBox, ElMessage, ElSwitch } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { DictDirection } from '@/store/modules/dicts'
import { getList, delData } from '@/api/BusinessConfig/serviceProvider'

const setForm = defineAsyncComponent(() => import('./components/setForm.vue'))

const { t } = useI18n()
let // 选中数组
  ids = [],
  // 选中服务商代码
  providerServiceCodes = [],
  // 非多个禁用
  multiple = ref(true),
  // 非单个禁用
  single = ref(true),
  title = ref(null),
  open = ref(false),
  customContainerRef = ref(null),
  setFormRef = ref(null),
  {
    proxy,
  }:{
    proxy:any
  } = getCurrentInstance()
// 查询
const 
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    providerServiceCode: undefined,
    serviceSimpleName: undefined,
    prooviderContact: undefined,
    status: undefined,
  })
   
// 多选
const handleTableSelect = (rows: { [key: string]: any }[]) => {
  ids = rows.map((item) => item.id)
  providerServiceCodes = rows.map(item => item.providerServiceCode)
  single.value = rows.length != 1
  multiple.value = !rows.length
}
// 特殊新增
const hadndAdd = () => {
  title.value = '新增服务商'
  open.value = true
}

// 修改
const handleUpdate = (row: { [key: string]: any }) => {
  const id = row.id
  // if (!Array.isArray(id)) {
  //   id = [id]
  // }
  title.value = '修改服务商'
  open.value = true
  nextTick(() => {
    setFormRef.value.getDetail(id)
  })
}

// 删除
const handleDelete = (row: { [key: string]: any }) => {
  const id = row?.id || ids
  const providerServiceCode = row.providerServiceCode || providerServiceCodes
  ElMessageBox.confirm(`此操作将永久删除服务商代码为"${ providerServiceCode }"的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(() => delData(id)).then(res => {
    if (res.code == 200) {
      ElMessage.success('删除成功')
      handleQuery()
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch(error => {
    if (error == 'cancel') {
      ElMessage.info('取消删除')
    }
  })
}
const handleQuery = () => {
  queryParams.value.pageNum = 1
  customContainerRef.value.getList()
}
const mainConfig = computed(() => {
  const queryField = 'BusinessConfiguration.serviceProviderManagement.queryField.'
  const btns = 'BusinessConfiguration.serviceProviderManagement.btns.'
  const columns = 'BusinessConfiguration.serviceProviderManagement.columns.'
  return {
    query: {
      fields: [
        {
          label: t(`${ queryField }providerServiceCode`),
          prop: 'providerServiceCode',
          option: {
            placeholder: '请输入服务商代码',
             
          },
        },
        {
          label: t(`${ queryField }serviceSimpleName`),
          prop: 'serviceSimpleName',
          option: { 
            placeholder: '请输入服务商简称',
          },
        },
        {
          label: t(`${ queryField }prooviderContact`),
          prop: 'prooviderContact',
          option: {
            placeholder: '请输入服务商联系人',
          },
        },
        {
          label: t(`${ queryField }status`),
          prop: 'status',
          type: 'select',
          option: {
            placeholder: '服务商状态',
            clearable: true,
            dictInfo: { dict: DictDirection.通用状态 },
          },
        },
					
      ],
      queryParams: queryParams.value,
    },
    btns: [
      {
        title: t(`${ btns }specialAdd`),
        type: 'primary',
        onClick: hadndAdd,
			
      },
      {
        title: t(`${ btns }edit`),
        type: 'primary',
        disabled: single.value,
        onClick: handleUpdate,
      },
      {
        title: t(`${ btns }del`),
        type: 'danger',
        disabled: multiple.value,
        onClick: handleDelete,
      },
    ],
    table: {
      columns: [
        { type: 'selection', width: '55', align: 'center' },
        {
          label: t(`${ columns }providerServiceCode`),
          prop: 'providerServiceCode',
          width: '100',
          align: 'center',
        },
        {
          label: t(`${ columns }serviceSimpleName`),
          prop: 'serviceSimpleName',
          width: '100',
          align: 'center',
        },
        {
          label: t(`${ columns }serviceWholeName`),
          prop: 'serviceWholeName',
          width: '180',
          align: 'center',
        },
        {
          label: t(`${ columns }providerContact`),
          prop: 'providerContact',
          width: '180',
          align: 'center',
        },
        {
          label: t(`${ columns }exchangeRate`),
          prop: 'exchangeRate',
          align: 'center',
        },
        { label: t(`${ columns }paymentAccount`),
          prop: 'paymentAccount',
          width: '180',
          align: 'center',
        },
        {
          label: t(`${ columns }bank`),
          prop: 'bank',
          width: '180',
          align: 'center',
        },
					
        {
          label: t(`${ columns }identificationNumber`),
          prop: 'identificationNumber',
          width: '180',
          align: 'center',
        },
					
        {
          label: t(`${ columns }status`),
          prop: 'status',
          align: 'center',
          width: '100',
          autoFormatter: {
            dict: DictDirection.通用状态,
            rowKey: 'status',
          },
        },
        {
          label: t(`${ columns }settlementMode`),
          prop: 'settlementMode',
          align: 'center',
          width: '100',
        },
        {
          label: t(`${ columns }type`),
          prop: 'type',
          align: 'center',
          width: '100',
        },
					
        {
          label: t(`${ columns }affiliation`),
          prop: 'affiliation',
          align: 'center',
          width: '100',
        },
					
        {
          label: t(`${ columns }openingDate`),
          prop: 'openingDate',
          align: 'center',
          width: '100',
        },
        {
          label: t(`${ columns }obsoleteDate`),
          prop: 'obsoleteDate',
          align: 'center',
          width: '100',
        },
        {
          label: t(`${ columns }operation`),
          align: 'center',
          width: '200',
          fixed: 'right',
          showOverflowTooltip: false,
          className: 'custormFixedCol',
          render: (h, params) => {
            const buttons = [
              {
                title: t(`${ btns }edit`),
                clickFun: handleUpdate,
								
              },
              {
                title: t(`${ btns }del`),

                clickFun: handleDelete,
									
              },
								
            ]
            return buttons.map((el) => h(
              ElButton,
              {
                text: true,
                type: 'primary',

                onClick: () => {
                  el.clickFun(params.row)
                },
              },
              {
                default: () => el.title,
              },
            ))
          },
        },
      ],
      event: {
        onSelectionChange: (rows) => {
          handleTableSelect(rows)
        },
      },
    },
    api: getList,
    customKey: 'BusinessConfiguration/serviceProviderManagement',
  }
})
</script>
